package com.smart.product.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smart.product.common.constants.ProductConstants;
import com.smart.product.entity.Product;

public interface ProductMapper extends BaseMapper<Product> {
    /**
     * @return
     *
     */
    default IPage<Product> selectList(String name, Long categoryId, Integer page, Integer size, Integer sort) {
        LambdaQueryWrapper<Product> lqw = new LambdaQueryWrapper<>();
        lqw.select(Product::getProductId, Product::getName, Product::getPic, Product::getPrice, Product::getSale)
                .ge(Product::getStatus, 1);
        lqw.like(Product::getName, name);
        if (categoryId != null) {
            lqw.eq(Product::getProductCategoryId, categoryId);
        }
        if (sort == ProductConstants.ProductSortType.SORT_DEFAULT) {
            lqw.orderByDesc(Product::getProductId);
        } else if (sort == ProductConstants.ProductSortType.SORT_SALE) {
            lqw.orderByAsc(Product::getSale);
        } else if (sort == ProductConstants.ProductSortType.SORT_PRICE_ASC) {
            lqw.orderByAsc(Product::getPrice);

        } else if (sort == ProductConstants.ProductSortType.SORT_PRICE_DESC) {
            lqw.orderByDesc(Product::getPrice);
        }
        return this.selectPage(new Page<>(page, size), lqw);
    }
}